package com.tongji.diy.model.furniturePlacementOptimization.assembleComplement
{
	import com.tongji.diy.model.furniturePlacementOptimization.OptimizationParameter;
	
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class BeddingBlock extends FurnitureBlock
	{
		private var _beddingList:Vector.<FurnitureBase> = new Vector.<FurnitureBase>;
		private var _blockWidth:Number;
		private var _blockLength:Number;
		
		public function BeddingBlock(furnitrueIndex:int, furnitureClass:int, aganistWall:Boolean, beddingList:Vector.<FurnitureBase>, length:Number=0, width:Number=0, height:Number=0)
		{
			super(furnitrueIndex, furnitureClass, aganistWall, length, width, height);
			this._beddingList = beddingList;
			main();
		}
		
		private function main():void
		{
			_blockWidth = (_beddingList.length + 1) * OptimizationParameter.ACCESS_DIS_FOR_BEDDING;
			_blockLength = 0;
			for(var i:int = 0; i < _beddingList.length; ++i)
			{
				_blockWidth += _beddingList[i].width;
				if(_blockLength < _beddingList[i].length)
					_blockLength = _beddingList[i].length;
			}
			
			_blockLength += OptimizationParameter.ACCESS_DIS_FOR_BEDDING;
			var tempX:Number = -_blockWidth/2;
			for(i = 0; i < _beddingList.length; ++i)
			{
				_beddingList[i].x = tempX + _beddingList[i].width/2 + OptimizationParameter.ACCESS_DIS_FOR_BEDDING;
				_beddingList[i].y = -_beddingList[i].length/2 + _blockLength/2;
				tempX += _beddingList[i].width + 2*OptimizationParameter.ACCESS_DIS_FOR_BEDDING;
			}
		}
		
		override public function get width():Number
		{
			return _blockWidth;
		}
		
		override public function get length():Number
		{
			return _blockLength;
		}
		public override function set x(newX:Number):void
		{
			var tempX:Number = super.x;
			tempX = newX - tempX;
			super.x = newX;
			
			for(var i:int = 0; i < _beddingList.length; ++i)
			{
				_beddingList[i].x += tempX;
			}
			_accessibleBox = buildAccessOBB(x,y,_blockWidth,_blockLength);
		}
		
		public override function set y(newY:Number):void
		{
			var tempY:Number = super.y;
			tempY = newY - tempY;
			super.y = newY;
			
			for(var i:int = 0; i < _beddingList.length; ++i)
			{
				_beddingList[i].y += tempY;
			}
			_accessibleBox = buildAccessOBB(x,y,_blockWidth,_blockLength);
		}
		
		public override function get furVector():Vector.<FurnitureBase>
		{
			return _beddingList;
		}
		public override function set angle(newAngle:Number):void
		{
			var tempangle:Number = super.angle;
			super.angle = newAngle;
			
			var m1:Matrix = new Matrix;
			var m2:Matrix = new Matrix;
			var r:Matrix = new Matrix;
			r.rotate(-newAngle + tempangle);
			m1.identity();
			m2.identity();
			m1.translate(-this.x,-this.y);
			m2.translate(this.x,this.y);
			m1.concat(r);
			m1.concat(m2);
			for(var i:int = 0; i < _beddingList.length; ++i)
			{	
				var fur:FurnitureBase = _beddingList[i];
				var resultPoint:Point  = m1.transformPoint(new Point(fur.x,fur.y));
				
				fur.x = resultPoint.x;
				fur.y = resultPoint.y;
				fur.angle += newAngle - tempangle;
			}
			
			_accessibleBox = buildAccessOBB(x,y,_blockWidth,_blockLength);
		}
	}
}